#!/bin/bash

set -e

baseDir="/environment/ace/python"
workDir="/environment/ace/python/ace/llm"


# 处理bom 特殊字符
cd $baseDir && python3 bom_resolver.py $workDir/models

# 备份文件
cd $baseDir && python3 encrypt_handler.py backup $workDir/models $workDir/models_backup --size 32
cd $baseDir && python3 encrypt_handler.py backup $workDir/configs $workDir/configs_backup --size 32
cd $baseDir && python3 encrypt_handler.py backup $workDir/constrained $workDir/constrained_backup --size 32
cd $baseDir && python3 encrypt_handler.py backup $workDir/entrypoints $workDir/entrypoints_backup --size 32
# 执行加密ace操作
# 1.进入目录
# 2.执行加密
cd $workDir && pyarmor -d gen -O dist -r -i models  && rm -rf models && mv dist/models . && rm -rf dist
cd $workDir && pyarmor -d gen -O dist -r -i configs  && rm -rf configs && mv dist/configs . && rm -rf dist
cd $workDir && pyarmor -d gen -O dist -r -i constrained  && rm -rf constrained && mv dist/constrained . && rm -rf dist
cd $workDir && pyarmor -d gen -O dist -r -i entrypoints  && rm -rf entrypoints && mv dist/entrypoints . && rm -rf dist

# 还原
# 1. 大于32kb的还原
# 2. 删除备份文件夹
cd $baseDir && python3 encrypt_handler.py restore $workDir/models_backup $workDir/models && rm -rf models_backup
cd $baseDir && python3 encrypt_handler.py restore $workDir/configs_backup $workDir/configs && rm -rf configs_backup
cd $baseDir && python3 encrypt_handler.py restore $workDir/constrained_backup $workDir/constrained && rm -rf constrained_backup
cd $baseDir && python3 encrypt_handler.py restore $workDir/entrypoints_backup $workDir/entrypoints && rm -rf entrypoints_backup

# 删除其他文件
cd $baseDir && rm -f bom_resolver.py && rm -f encrypt_handler.py
rm -fr $workDir/models_backup
rm -fr $workDir/configs_backup
rm -fr $workDir/constrained_backup
rm -fr $workDir/entrypoints_backup